#include <Misc/Config.hpp>


Config *Config::configInstance = 0;

Config::Config(){

	setDefault();

}

Config *Config::instance(){
	if(Config::configInstance == 0){
		Config::configInstance = new Config();
	}
	return Config::configInstance;
}

void Config::parseArguments(int argc, char **argv){

	if(argc > 1){

		for(int i=1; i<argc; i++){

			std::string arg(argv[i]);

			if(!arg.compare("-pcl") || !arg.compare("--port-client-listen")){

				std::istringstream(argv[i+1]) >> childClientPort;

			}else if(!arg.compare("-psl") || !arg.compare("--port-server-listen")){

				std::istringstream(argv[i+1]) >> childServerPort;

			}else if(!arg.compare("-psc") || !arg.compare("--port-server-connect")){

				std::istringstream(argv[i+1]) >> parentServerPort;

			}else if(!arg.compare("-a") || !arg.compare("--address")){

				std::istringstream(argv[i+1]) >> address4;
				address6 = address4;

			}else if(!arg.compare("-n") || !arg.compare("--name")){

				std::istringstream(argv[i+1]) >> serverName;

			}else if(!arg.compare("-i") || !arg.compare("--info-file")){

				std::istringstream(argv[i+1]) >> serverInformationFile;

			}else if(!arg.compare("-r") || !arg.compare("--raw-data-log-file")){

				std::istringstream(argv[i+1]) >> rawDataLogFile;

			}else if(!arg.compare("-l") || !arg.compare("--log-file")){

				std::istringstream(argv[i+1]) >> logFile;

			}else if(!arg.compare("-c")){

				connectToServerFlag = true;

			}else if(!arg.compare("-IPv6")){

				ipv6Flag = true;

			}else if(!arg.compare("-h") || !arg.compare("--help")){

				helpFlag = true;

			}

		}

	}else{
		/*
		 * Set default values
		 */
		setDefault();
	}


}

void Config::setDefault(){

	address4 = "127.0.0.1";
	address6 = "::1";
	parentServerPort = 6001;
	childServerPort = 6000;
	childClientPort = 5000;

	rawDataLogFile = "/tmp/ibrc-server.raw";
	serverInformationFile = "/tmp/ibrc-server.info";
	logFile = "/tmp/ibrc-server.log";

	connectToServerFlag = false;
	helpFlag = false;
	ipv6Flag = false;

}

std::string& Config::getAddress(){
	if(ipv6Flag){
		return address6;
	}else{
		return address4;
	}
}

std::string& Config::getAddress4(){
	return address4;
}

std::string& Config::getAddress6(){
	return address6;
}

std::string& Config::getServerName(){
	return serverName;
}

int Config::getParentServerPort(){
	return parentServerPort;
}

int Config::getChildServerPort(){
	return childServerPort;
}

int Config::getChildClientPort(){
	return childClientPort;
}

std::string& Config::getRawDataLogFile(){
	return rawDataLogFile;
}

std::string& Config::getServerInformationFile(){
	return serverInformationFile;
}

std::string& Config::getLogFile(){
	return logFile;
}

bool Config::isHelpFlag(){
	return helpFlag;
}

bool Config::isIPv6Flag(){
	return ipv6Flag;
}

bool Config::isConnectToServerFlag(){
	return connectToServerFlag;
}
